﻿using DevExpress.Utils;
using System;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace Smart.Common
{
    public class Options
    {
        private static XProgressForm _ProgressForm = new XProgressForm();
        private static string _SoftId;

        public static string SoftId
        {
            get { return Options._SoftId; }
            set { Options._SoftId = value; }
        }
        private static int _TypeSoft;

        public static int TypeSoft
        {
            get { return Options._TypeSoft; }
            set { Options._TypeSoft = value; }
        }
        private static string m_Caption = "Đang nạp dữ liệu ...";
        private static WaitDialogForm m_Dlg = null;
        private static string m_FTitle = "Vui lòng đợi trong giây lát...";

        static Options()
        {
            SoftId = "QLK";
            TypeSoft = 0;
        }
        public static void Close()
        {
            CloseDialog();
        }

        public static void CloseDialog()
        {
            if (m_Dlg != null)
            {
                m_Dlg.FormClosing += new FormClosingEventHandler(Options.dlg_FormClosing);
                m_Dlg.Close();
            }
        }

        public static void CreateWaitDialog()
        {
            CreateWaitDialog(Caption);
        }

        public static void CreateWaitDialog(string fCaption)
        {
            CreateWaitDialog(fCaption, Title);
        }

        public static void CreateWaitDialog(string fCaption, string fTitle)
        {
            Title = fTitle;
            if (m_Dlg == null)
            {
                m_Dlg = new WaitDialogForm(fCaption, fTitle);
            }
            m_Dlg.AllowFormSkin = true;
        }

        private static void dlg_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Dlg = null;
        }

        ~Options()
        {
            if (m_Dlg != null)
            {
                m_Dlg.FormClosing += new FormClosingEventHandler(Options.dlg_FormClosing);
                m_Dlg.Close();
            }
        }

        public static void HideDialog()
        {
            if (m_Dlg != null)
            {
                m_Dlg.Hide();
            }
        }

        public static void SetWaitDialogCaption()
        {
            SetWaitDialogCaption(Caption);
        }

        public static void SetWaitDialogCaption(string fCaption)
        {
            Caption = fCaption;
            if (m_Dlg != null)
            {
                m_Dlg.Visible = true;
                m_Dlg.Caption = fCaption;
            }
            else
            {
                CreateWaitDialog();
            }
        }

        public static void Show()
        {
            SetWaitDialogCaption();
        }

        public static void Show(string fCaption)
        {
            SetWaitDialogCaption(fCaption);
        }

        public static string Caption
        {
            get
            {
                return m_Caption;
            }
            set
            {
                m_Caption = value;
            }
        }

        public static XProgressForm FormProcess
        {
            get
            {
                return _ProgressForm;
            }
            set
            {
                _ProgressForm = value;
            }
        }

        public static string Title
        {
            get
            {
                return m_FTitle;
            }
            set
            {
                m_FTitle = value;
            }
        }

        public class DateTime
        {
            private static Smart.Common.Options.FormatType m_DateFormatType = Smart.Common.Options.FormatType.Custom;
            private static string m_DateSaparator = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
            private static int m_DayLength = 2;
            private static string m_FormatDate = "dd/MM/yyyy";
            private static int m_MonthLength = 2;
            private static int m_YearLength = 4;

            private static string GetDay(bool DateSaparator)
            {
                string str = "";
                for (int i = 0; i < m_DayLength; i++)
                {
                    str = str + "d";
                }
                if (DateSaparator)
                {
                    str = str + m_DateSaparator;
                }
                return str;
            }

            private static string GetMonth(bool DateSaparator)
            {
                string str = "";
                for (int i = 0; i < m_MonthLength; i++)
                {
                    str = str + "M";
                }
                if (DateSaparator)
                {
                    str = str + m_DateSaparator;
                }
                return str;
            }

            private static string GetYear(bool DateSaparator)
            {
                string str = "";
                for (int i = 0; i < m_YearLength; i++)
                {
                    str = str + "y";
                }
                if (DateSaparator)
                {
                    str = str + m_DateSaparator;
                }
                return str;
            }

            public static int DayLength
            {
                get
                {
                    return m_DayLength;
                }
                set
                {
                    m_DayLength = value;
                }
            }

            public static string Format
            {
                get
                {
                    string day;
                    string month;
                    string year;
                    if (m_DateFormatType == Smart.Common.Options.FormatType.DMY)
                    {
                        day = GetDay(true);
                        month = GetMonth(true);
                        year = GetYear(false);
                        m_FormatDate = (day.Length == 1) ? "" : (day + ((month.Length == 1) ? "" : month) + year);
                        if (m_FormatDate.Trim() == string.Empty)
                        {
                            m_FormatDate = "dd/MM/yyyy";
                        }
                        return m_FormatDate;
                    }
                    if (m_DateFormatType == Smart.Common.Options.FormatType.MDY)
                    {
                        day = GetDay(true);
                        month = GetMonth(true);
                        year = GetYear(false);
                        m_FormatDate = (month.Length == 1) ? "" : (month + ((day.Length == 1) ? "" : day) + year);
                        if (m_FormatDate.Trim() == string.Empty)
                        {
                            m_FormatDate = "MM/dd/yyyy";
                        }
                        return m_FormatDate;
                    }
                    if (m_DateFormatType == Smart.Common.Options.FormatType.YMD)
                    {
                        day = GetDay(false);
                        month = GetMonth(true);
                        year = GetYear(true);
                        m_FormatDate = (year.Length == 1) ? "" : (year + ((month.Length == 1) ? "" : month) + day);
                        if (m_FormatDate.Trim() == string.Empty)
                        {
                            m_FormatDate = "yyyy/MM/dd";
                        }
                        return m_FormatDate;
                    }
                    return m_FormatDate;
                }
                set
                {
                    m_DateFormatType = Smart.Common.Options.FormatType.Custom;
                    m_FormatDate = value;
                }
            }

            public static string FormatString
            {
                get
                {
                    return ("{0:" + Format + "}");
                }
            }

            public static Smart.Common.Options.FormatType FormatType
            {
                get
                {
                    return m_DateFormatType;
                }
                set
                {
                    m_DateFormatType = value;
                }
            }

            public static int MonthLength
            {
                get
                {
                    return m_MonthLength;
                }
                set
                {
                    m_MonthLength = value;
                }
            }

            public static string Saparator
            {
                get
                {
                    return m_DateSaparator;
                }
                set
                {
                    m_DateSaparator = value;
                }
            }

            public static int YearLength
            {
                get
                {
                    return m_YearLength;
                }
                set
                {
                    m_YearLength = value;
                }
            }
        }

        public enum FormatType
        {
            Custom,
            DMY,
            MDY,
            YMD
        }

        public class Number
        {
            private static bool m_AutoRound = true;
            private static int m_Decimals = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalDigits;
            private static string m_DecimalSymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
            private static string m_DigitGroupSymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator;
            private static string m_FormatNumber = "##,##0.###";
            private static string m_FrefixSymbol = "";
            private static string m_SuffixSymbol = "";

            public static bool AutoRound
            {
                get
                {
                    return m_AutoRound;
                }
                set
                {
                    m_AutoRound = value;
                }
            }

            public static int Decimals
            {
                get
                {
                    return m_Decimals;
                }
                set
                {
                    m_Decimals = value;
                }
            }

            public static string DecimalSymbol
            {
                get
                {
                    return m_DecimalSymbol;
                }
                set
                {
                    m_DecimalSymbol = value;
                }
            }

            public static string DigitGroupSymbol
            {
                get
                {
                    return m_DigitGroupSymbol;
                }
                set
                {
                    m_DigitGroupSymbol = value;
                }
            }

            public static string Format
            {
                get
                {
                    string str = "##,##0.";
                    for (int i = 0; i < m_Decimals; i++)
                    {
                        str = str + (m_AutoRound ? "#" : "0");
                    }
                    m_FormatNumber = m_FrefixSymbol + str + m_SuffixSymbol;
                    return m_FormatNumber;
                }
                set
                {
                    m_FormatNumber = value;
                }
            }

            public static string FormatString
            {
                get
                {
                    return ("{0:" + Format + "}");
                }
            }

            public static string FrefixSymbol
            {
                get
                {
                    return m_FrefixSymbol;
                }
                set
                {
                    m_FrefixSymbol = value;
                }
            }

            public static string SuffixSymbol
            {
                get
                {
                    return m_SuffixSymbol;
                }
                set
                {
                    m_SuffixSymbol = value;
                }
            }
        }

        public class Parameter
        {
            private static string m_Manager = "Giám đốc";

            public static string Manager
            {
                get
                {
                    return m_Manager;
                }
                set
                {
                    m_Manager = value;
                }
            }
        }

        public class Regional
        {
            private static Options.FormatType m_DateFormatType = Options.FormatType.Custom;
            private static string m_DateSaparator = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
            private static int m_Decimals = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalDigits;
            private static string m_DecimalSymbol = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
            private static string m_DigitGroupSymbol = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator;
            private static string m_FormatDate = "dd/MM/yyyy";
            private static string m_FormatNumber = "##,##0.###";
            private static bool m_IsMsgBoxResult = true;
            private static bool m_IsQuestion = true;
            private static int m_NumberDecimalDigits = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalDigits;

            public static Options.FormatType DateFormatType
            {
                get
                {
                    return m_DateFormatType;
                }
                set
                {
                    m_DateFormatType = value;
                }
            }

            public static string DateSaparator
            {
                get
                {
                    return m_DateSaparator;
                }
                set
                {
                    m_DateSaparator = value;
                }
            }

            public static int Decimals
            {
                get
                {
                    return m_Decimals;
                }
                set
                {
                    m_Decimals = value;
                }
            }

            public static string DecimalSymbol
            {
                get
                {
                    return m_DecimalSymbol;
                }
                set
                {
                    m_DecimalSymbol = value;
                }
            }

            public static string DigitGroupSymbol
            {
                get
                {
                    return m_DigitGroupSymbol;
                }
                set
                {
                    m_DigitGroupSymbol = value;
                }
            }

            public static string FormatDate
            {
                get
                {
                    if (m_DateFormatType == Options.FormatType.DMY)
                    {
                        m_FormatDate = Options.FormatType.DMY.ToString().Replace("/", m_DateSaparator);
                        return m_FormatDate;
                    }
                    if (m_DateFormatType == Options.FormatType.MDY)
                    {
                        m_FormatDate = Options.FormatType.MDY.ToString().Replace("/", m_DateSaparator);
                        return m_FormatDate;
                    }
                    if (m_DateFormatType == Options.FormatType.YMD)
                    {
                        m_FormatDate = Options.FormatType.YMD.ToString().Replace("/", m_DateSaparator);
                        return m_FormatDate;
                    }
                    return m_FormatDate;
                }
                set
                {
                    m_DateFormatType = Options.FormatType.Custom;
                    m_FormatDate = value;
                }
            }

            public static string FormatNumber
            {
                get
                {
                    string str = "##,##0.";
                    for (int i = 0; i < m_Decimals; i++)
                    {
                        str = str + "#";
                    }
                    m_FormatNumber = str;
                    return str;
                }
                set
                {
                    m_FormatNumber = value;
                }
            }

            public static bool IsMsgBoxResult
            {
                get
                {
                    return m_IsMsgBoxResult;
                }
                set
                {
                    m_IsMsgBoxResult = value;
                }
            }

            public static bool IsQuestion
            {
                get
                {
                    return m_IsQuestion;
                }
                set
                {
                    m_IsQuestion = value;
                }
            }

            public static int NumberDecimalDigits
            {
                get
                {
                    return m_NumberDecimalDigits;
                }
                set
                {
                    m_NumberDecimalDigits = value;
                }
            }

            public static string StringFormat
            {
                get
                {
                    return ("{0:" + FormatNumber + "}");
                }
            }
        }

        public class System2
        {
            private static bool m_IsMsgBoxError = true;
            private static bool m_IsMsgBoxResult = true;
            private static bool m_IsQuestion = true;

            public static bool IsMsgBoxError
            {
                get
                {
                    return m_IsMsgBoxError;
                }
                set
                {
                    m_IsMsgBoxError = value;
                }
            }

            public static bool IsMsgBoxResult
            {
                get
                {
                    return m_IsMsgBoxResult;
                }
                set
                {
                    m_IsMsgBoxResult = value;
                }
            }

            public static bool IsQuestion
            {
                get
                {
                    return m_IsQuestion;
                }
                set
                {
                    m_IsQuestion = value;
                }
            }
        }
    }
}